package com.rpframework.website.device.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.rpframework.core.BaseService;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.dao.ICollectDao;
import com.rpframework.website.device.domain.TCollect;
import com.rpframework.website.device.util.HConstants;

@Service
public class CollectService extends BaseService{
	public @Resource ICollectDao iDao;
	
	public Pager<TCollect> getPager(Pager<TCollect> pager) {
		long startTime = System.currentTimeMillis();
		List<TCollect> itemList = iDao.doPager(this.packageMyBatisParam(pager));
		pager.setItemList(itemList);
		pager.setCostTime(System.currentTimeMillis()-startTime);
		return pager;
	}

	public Boolean collectByGoods(Integer userId, Integer goodsId) {
		TCollect t = iDao.doCollectByGoods(userId,goodsId);
		boolean f = false;
		if(t!=null){
			if(t.getState()==1){
				t.setState(2);
				f = iDao.update(t);
				return !f;//取消收藏
			}else{
				t.setState(1);
				f = iDao.update(t);
				return f;//收藏
			}
		}else{
			t = new TCollect();
			t.setUserId(userId);
			t.setGoodsId(goodsId);
			t.setState(HConstants.EntityState.STATE_NORMAL);
			t.setCreateTime(System.currentTimeMillis()/1000);
			f = iDao.insert(t);
			return f;
		}
	}

	public TCollect getCollectByUserGoods(Integer userId, Integer goodsId) {
		return iDao.doCollectByGoods(userId, goodsId);
	}
}
